//
//  MyTextField.swift
//  MyKebiao
//
//  Created by David on 2016/12/2.
//  Copyright © 2016年 David. All rights reserved.
//

import UIKit

enum UDTextFieldType {
    case email
    case password
    case verify
}

class UDTextField: UITextField {
    
    fileprivate var underLine:UIView!
    var tabIndex:Int = 0
    var underLineColor:UIColor{
        get{
            return underLine.backgroundColor!
        }
        set(val){
            underLine.backgroundColor = val
        }
    }
    fileprivate var inputType:UDTextFieldType = .email
    var textFieldType:UDTextFieldType{
        get{
            return inputType
        }
        set(val){
            inputType = val
            setType()
        }
    }
    
    var boderWidth:CGFloat{
        get{
            return underLine.frame.height
        }
        set(value){
            underLine.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: value)
            underLine.center.y = self.frame.height - value/2
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.borderStyle = UITextBorderStyle.none
        self.clearButtonMode = .whileEditing
        underLine = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: 2))
        underLine.center.y = self.frame.height - 1
        underLine.backgroundColor = UIColor.black
        self.addSubview(underLine)
        setType()
    }
    
    fileprivate func setType(){
        switch inputType {
        case .email:
            placeholder = "用户名"
            keyboardType = .emailAddress
            returnKeyType = .next
            autocorrectionType = .no
            autocapitalizationType = .none
            tabIndex = 0
            break
        case .password:
            placeholder = "密码"
            isSecureTextEntry = true
            keyboardType = .asciiCapable
            returnKeyType = .next
            tabIndex = 1
            break
        case .verify:
            placeholder = "验证码"
            returnKeyType = .done
            keyboardType = .asciiCapable
            autocorrectionType = .no
            autocapitalizationType = .none
            tabIndex = 2
            break
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    /*
     // Only override drawRect: if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func drawRect(rect: CGRect) {
     // Drawing code
     }
     */
    
}
